package office.stock.presentation;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import office.stock.model.Offer;
import office.stock.model.SupportedCurrency;
import office.stock.model.User;
import office.stock.security.SessionAttributes;
import office.stock.services.ImageStorage;
import office.stock.services.OffersService;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ManagedBean
@SessionScoped
public class OfferBackingBean implements Serializable {

    private static final long serialVersionUID = 1L;

    private final static transient Log log = LogFactory.getLog(OfferBackingBean.class);
    
    @EJB
    private OffersService offerService;
    
    @EJB
    private ImageStorage imageStorage;
    
    private String title;
    private String description;
    private Double price;
    private SupportedCurrency currency;
    private String imageId;
    
    
    public List<SupportedCurrency> getSupportedCurrencyList() {
	return Arrays.asList(SupportedCurrency.values());
    }
    
    //TODO add ability to edit own offers
    //TODO make some refactoring (javascript in particular)
    
    public String convertTextToHTML(String text) {
	if (text == null) {
	    return null;
	}
	return text.replace("\n", "<br />");
    }
    
    public String createOffer() {
	log.debug("Creating new offer " + title);
	User currentUser = getCurrentUser();
	Offer offer = new Offer(title, price, description, currentUser);
	offer.setImageId(imageId);
	offer.setCurrency(currency);
	offerService.addNewOffer(offer);
	
	log.debug(description);
	
	clearFields();
	return "offerCreated";
    }

    public String cancelOrderCreation() {
	removeImage();
	return "offerCannceled";
    }

    public void removeImage() {
	log.debug("removing image " + imageId + " from image storage " + imageStorage);
	imageStorage.remove(imageId);
	imageId = null;
    }
    
    
    private User getCurrentUser() {
	User currentUser = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get(SessionAttributes.USER.name());
	if (currentUser == null) {
	    throw new RuntimeException("User is not logged in");
	}
	return currentUser;
    }

    public void remove(Long id) {
	log.debug("removing offer " + id);
	if (id != null) {
	    offerService.remove(id);
	}
    }
    
    public void buy(Long offerId) {
	log.debug("buying some stuff with id " + offerId);
	offerService.buy(offerId, getCurrentUser());
    }
    
    public List<Offer> getAll() {
	return offerService.getAll();
    }
    
    private void clearFields() {
	title = null;
	description = null;
	price = null;
	imageId = null;
	currency = null;
    }
    
    public String getDescription() {
	return description;
    }

    public void setDescription(String description) {
	this.description = description;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public String getTitle() {
	return title;
    }

    public void setTitle(String title) {
	this.title = title;
    }

    public String getImageId() {
        return imageId;
    }

    public void setImageId(String imageId) {
        this.imageId = imageId;
    }

    public SupportedCurrency getCurrency() {
        return currency;
    }

    public void setCurrency(SupportedCurrency currency) {
        this.currency = currency;
    }
    
}
